package com.cx.java;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.cx.java.JdbcUtils;

/**
 * Servlet implementation class UpdateServlet
 */
@WebServlet("/update")
public class UpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		String id = request.getParameter("id");
		String carName = request.getParameter("carname");
		String carPrice = request.getParameter("carprice");
		String carLoadtime = request.getParameter("carloadtime");
		String carMileage = request.getParameter("carmileage");
		String carGearbox = request.getParameter("cargearbox");
		String carVolume = request.getParameter("carvolume");
		String sql = "update car set carName='" + carName + "',price='"+ carPrice
				+ "',carLoadtime='" + carLoadtime + "',mileage='" + carMileage + "'," 
				+ "gearbox='" + carGearbox + "', volume='" + carVolume + "' where id=" + id;
		System.out.println(sql);
		JdbcUtils ju = JdbcUtils.getInstance();
		int affectedRow = ju.executeUpdate(sql);
		System.out.println(sql);
		System.out.println("修改成功" + affectedRow);
		if(affectedRow > 0) {
			response.sendRedirect("/car/CarInfoServlet");
		} else {
			response.sendRedirect("/car/Details?id="+id);
		}
				
		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

